#Boa:Dialog:BuscarDescrip

import wx
import wx.grid
import consultasBD

def create(parent):
    return BuscarDescrip(parent)

[wxID_BUSCARDESCRIP, wxID_BUSCARDESCRIPGRID1, wxID_BUSCARDESCRIPINBUSCAR, 
 wxID_BUSCARDESCRIPINPUT_BUSCAR, wxID_BUSCARDESCRIPSTATICTEXT1, 
] = [wx.NewId() for _init_ctrls in range(5)]

class BuscarDescrip(wx.Dialog):
    def _init_ctrls(self, prnt):
        # generated method, don't edit
        wx.Dialog.__init__(self, id=wxID_BUSCARDESCRIP, name='BuscarDescrip',
              parent=prnt, pos=wx.Point(449, 177), size=wx.Size(704, 418),
              style=wx.DEFAULT_DIALOG_STYLE,
              title=u'Buscar en la descripci\xf3n')
        self.SetClientSize(wx.Size(696, 384))

        self.staticText1 = wx.StaticText(id=wxID_BUSCARDESCRIPSTATICTEXT1,
              label='Buscar que contenga:', name='staticText1', parent=self,
              pos=wx.Point(8, 8), size=wx.Size(88, 24), style=0)

        self.input_buscar = wx.TextCtrl(id=wxID_BUSCARDESCRIPINPUT_BUSCAR,
              name=u'input_buscar', parent=self, pos=wx.Point(88, 8),
              size=wx.Size(184, 21), style=0, value='')
        self.input_buscar.Bind(wx.EVT_KILL_FOCUS, self.OnInput_buscarKillFocus)

        self.inbuscar = wx.Button(id=wxID_BUSCARDESCRIPINBUSCAR,
              label=u'Buscar...', name='inbuscar', parent=self,
              pos=wx.Point(288, 8), size=wx.Size(75, 23), style=0)
        self.inbuscar.Bind(wx.EVT_BUTTON, self.OnInbuscarButton,
              id=wxID_BUSCARDESCRIPINBUSCAR)

        self.grid1 = wx.grid.Grid(id=wxID_BUSCARDESCRIPGRID1, name='grid1',
              parent=self, pos=wx.Point(8, 40), size=wx.Size(680, 336),
              style=0)
        self.grid1.Bind(wx.grid.EVT_GRID_CELL_LEFT_DCLICK,
              self.OnGrid1GridCellLeftDclick)

    def __init__(self, parent, proveedor):
        self._init_ctrls(parent)
        self.codsussan = ""
        self.codartprov = ""
        self.proveedor = proveedor
        self.grillafactura()
        
    def grillafactura(self):
        columnas =['Cod. Art. Prov.','Cod. Sussan','Descripcion                                                  ', 'P. Costo  ','Grupo                                      ']
        
        self.grid1.CreateGrid(300,len(columnas))
                
        for i, col in enumerate(columnas): 
            self.grid1.SetColLabelValue(i, col)
            self.grid1.AutoSizeColumn(i,True)
            
        #para hacer que solo sean legibles algunas columnas
        for x in xrange(300):
            self.grid1.AutoSizeRow(x,True)
            for i in xrange(len(columnas)):
                self.grid1.SetReadOnly(x,i,True)            
            
    def _limpiar_grilla(self):        
        for fil in range(300):
            for col in range(5):            
                self.grid1.SetCellValue(fil, col, "")                          
        
    def busca(self,codigo):
        self._limpiar_grilla()
        sql = "SELECT * FROM productos p JOIN producto_codartprov cb ON (p.codsussan = cb.codsussan) JOIN grupos g ON (p.grupo = g.idgrupo) WHERE (p.descri LIKE '%%%s%%' OR g.nombregrupo LIKE '%%%s%%') AND p.proveedor = '%s'" % (str(codigo), str(codigo), str(self.proveedor))
        #sql = "SELECT * FROM productos p JOIN grupos g ON (p.grupo = g.idgrupo) WHERE (p.descri LIKE '%%%s%%' OR g.nombregrupo LIKE '%%%s%%') AND p.proveedor = '%s'" % (str(codigo), str(codigo), str(self.proveedor))
        resultado = consultasBD.ordenCrudaDict(sql)
        
        print sql
        
##        max_codsussan = max([len(str(x['codsussan'])) for x in resultado])
##        max_codartprov   = max([len(str(x['codartprov'])) for x in resultado])
##        max_descri    = max([len(x['descri']) for x in resultado])
##        
##        print max_codsussan, max_codartprov, max_descri
        
        for i, r in enumerate(resultado):
            self.grid1.SetCellValue(i, 0, str(r['codartprov']))
            #self.grid1.SetCellValue(i, 0, str(r['codsussan']))
            self.grid1.SetCellValue(i, 1, str(r['codsussan']))
            self.grid1.SetCellValue(i, 2, str(r['descri']))
            self.grid1.SetCellValue(i, 3, str(r['preciocompra']))
            self.grid1.SetCellValue(i, 4, str(r['nombregrupo']))
    
    def Muestraid(self):
        print self.codsussan, self.codartprov
        return (self.codsussan, self.codartprov)

    def OnInbuscarButton(self, event):
        busqueda = self.input_buscar.Value
        self.busca(busqueda)

    def OnGrid1GridCellLeftDclick(self, event):      
        fila = self.grid1.GetGridCursorRow()
        id = self.grid1.GetCellValue(fila, 0)
        codsussan = self.grid1.GetCellValue(fila, 1)
        
        print "BUUUUUUUUUUUUUUUSCAR", id, codsussan
        
        self.codsussan = codsussan
        self.codartprov = id
        self.Close()        
        event.Skip()

    def OnInput_buscarKillFocus(self, event):
        busqueda = self.input_buscar.Value
        self.busca(busqueda)
        
        
